﻿import QtQuick 2.0
import com.lolClient 1.0

Item {
    id: mainRoot;
    property int scrMargin: 0;
    property var role: null;
    property int roleId: -1;
    property var members: []
    property MouseArea fullMa: null;
    signal reqQuit();
    
    focus: true;
    Component.onCompleted: init();
    
    function init(){
        mapLoader.init();
    }
    
    /*!构建本机角色
    */
    function buildCurRole(id){
        roleId = id;
        var cpt = Qt.createComponent("qrc:/MRoleLoader.qml");
        role = cpt.createObject(flick.contentItem,{"id": id,"isCur": true});
        role.request.connect(client.sendData);
        roleMap.add(role);
    }
    
    /*!构建其他角色
    */
    function buildOtherRole(obj){
        if( roleMap.has(obj.id) )
            return;
        var cpt = Qt.createComponent("qrc:/MRoleLoader.qml");
        cpt = cpt.createObject(flick.contentItem,{"id": obj.id,"defObj": obj});
        roleMap.add(cpt);
    }
    
    /*!发送角色同步数据
    */
    function sendRoleSyncConfirm(){
        var obj = {
            "cmd": "role_syncConfirm",
            "id": roleId,
            "data": roleMap.itemIds.join(",")+""
        }
        client.sendDataFun(JSON.stringify(obj));
    }
    
    /*!刷新角色状态
    */
    function refreshRole(datas){
        var cObj = null;
        var cItem = null;
        for(var i=0;i<datas.length;i++)
        {
            cObj = datas[i];
            cItem = roleMap.get(cObj.id);
            if( cItem && cItem != role )
                cItem.refreshSta(cObj);
        }
    }
    
    /*!删除退出角色
    */
    function removeRole(id){
        if( roleMap.has(id) ){
            roleMap.get(id).destroy();
        }
    }
    
    function mouseChange(){
        checkTimer.restart();
    }
    
    QtObject{
        id: roleMap;
        property var itemIds: [];
        property var items: [];
        
        function add(item){
            if( !has(item.id) ){
                itemIds.push(Number(item.id));
                items.push(item);
            }
        }
        
        function has(id){
            return (itemIds.indexOf(Number(id))>=0);
        }
        
        function get(id){
            if( has(id) ){
                return items[itemIds.indexOf(Number(id))];
            }
            return null;
        }
    }
    
    Timer{
        id: memberTimer;
        property var tasks: [];
        repeat: true;
        interval: 333;
        
        function add(objs){
            if( !objs || objs.length <=0 )
                return;
            for(var i=0;i<objs.length;i++)
            {
                if( roleMap.has(objs[i].id) )
                    continue;
                tasks.push(objs[i]);
            }
            restart();
        }
        
        onTriggered: {
            if( tasks.length <= 0 ){
                stop();
                return;
            }
            
            var firstObj = tasks[0];
            if( !roleMap.has(firstObj.id) ){
                buildOtherRole(firstObj);
            }
            
            tasks.splice(0,1);
            if( tasks.length <= 0 ){
                stop();
                //
                Qt.callLater(sendRoleSyncConfirm);
            }
        }
        
        
    }
    
    MClientMag{
        id: client;
        
        onNewCommand:{
            //jsObj
            
            if( jsObj )
            {
                //console.log("--cmd=",jsObj.cmd);
                if( jsObj.cmd === "init" )
                {//建立链接后，初始化
                    roleId = jsObj.id;
                    buildCurRole(jsObj.id);
                    Qt.callLater(sendRoleSyncConfirm);
                }
                else if( jsObj.cmd === "addMember" )
                {//添加成员
                    memberTimer.add(jsObj.data);
                }
                else if( jsObj.cmd === "removeMember" )
                {//有成员端口链接
                    removeRole(jsObj.id);
                }
            } 
        }
        
        onNewRefresh:{
            //刷新成员状态
            if( jsObj )
                refreshRole(jsObj.data);
        }
    }
    
    //    MClient{
    //        id: client;
    //        onNewCommand:{
    //            //jsObj
    
    //            if( jsObj )
    //            {
    //                //console.log("--cmd=",jsObj.cmd);
    //                if( jsObj.cmd === "init" )
    //                {//建立链接后，初始化
    //                    buildCurRole(jsObj.id);
    //                }
    //                else if( jsObj.cmd === "addMember" )
    //                {//添加成员
    //                    memberTimer.add(jsObj.data);
    //                }
    //                else if( jsObj.cmd === "removeMember" )
    //                {//有成员端口链接
    //                    removeRole(jsObj.id);
    //                }
    //            } 
    //        }
    
    //        onRefresh:{
    //            //刷新成员状态
    //            if( jsObj )
    //                refreshRole(jsObj.data);
    //        }
    //    }
    
    Keys.onReleased: {
        event.accepted = true;
        if(event.key === Qt.Key_Escape || event.key === Qt.Key_Back)
        { 
            reqQuit();
        }
    }
    
    Timer{
        id: checkTimer;
        property int minV: 4;
        property int xBegin: minV;
        property int xEnd: flick.width - minV;
        property int yBegin: minV;
        property int yEnd: flick.height - minV;
        property int stepLen: 60;
        interval: 45;
        repeat: true;
        onTriggered: checkChange();
        
        function checkChange(){
            var mx = fullMa.mouseX-scrMargin;
            var my = fullMa.mouseY-scrMargin;
            if( mx <= xBegin ){
                if( flick.contentX-stepLen <=0 )
                    flick.contentX = 0;
                else
                    flick.contentX -= stepLen;
            }
            else if( mx >= xEnd ){
                if( flick.contentX+stepLen >= (flick.contentWidth-flick.width) )
                    flick.contentX = flick.contentWidth-flick.width;
                else
                    flick.contentX += stepLen;
            }
            
            if( my <= yBegin ){
                if( flick.contentY-stepLen <=0 )
                    flick.contentY = 0;
                else
                    flick.contentY -= stepLen;
            }
            else if( my >= yEnd ){
                if( flick.contentY+stepLen >= (flick.contentHeight-flick.height) )
                    flick.contentY = flick.contentHeight-flick.height;
                else
                    flick.contentY += stepLen;
            } 
            
            if( flick.contentX <= 0 )
                flick.contentX = 0;
            if( flick.contentY <= 0 )
                flick.contentY = 0;
            
            if( (flick.contentX <=0 || flick.contentX >= (flick.contentWidth-flick.width))
                    && (flick.contentY <=0 || flick.contentY >= (flick.contentHeight-flick.height)) 
                    || (mx > xBegin && mx < xEnd && my > yBegin && my < yEnd)){
                checkTimer.stop();
            }
        }
    }
    
    Flickable{
        id: flick;
        anchors.fill: parent;
        contentHeight: height * 3;
        contentWidth: width * 3;
        boundsBehavior: Flickable.StopAtBounds;
        interactive: false;
        
        //地图
        Loader{
            id: mapLoader;
            width: flick.contentWidth;
            height: flick.contentHeight;
            function init(){
                if( item )
                    return;
                
                setSource("qrc:/QGameMap.qml",{"flick": flick});
            }
            
            onLoaded: {
                item.setRolePosition.connect(
                            function (x,y){
                                if( role ){
                                    role.setPosition(x,y);
                                }
                            }
                            );
            }
        }
        
    }
    
    /*!右下角的快照-预览
     */
    Loader{
        id: snapsShotsLoader;
        width: mainRoot.width*0.2;
        height: parent.height/parent.width*width;
        anchors.right: parent.right;
        anchors.bottom: parent.bottom;
        anchors.bottomMargin: 2;
        anchors.rightMargin: 2;
        asynchronous: true;
        Component.onCompleted: init();
        
        function init(){
            if( item )
                return;
            setSource("qrc:/MGameSnapsShots.qml",{"flick": flick,"gameCore": flick.contentItem})
        }
        
        onLoaded: {
            item.setRolePosition.connect(
                        function (xR,yR){
                            if( role )
                                role.setPosition(xR*flick.contentWidth,yR*flick.contentHeight);
                        }
                        );
        }
    }
    
}
